package com.cskaoyan.service;

import com.cskaoyan.bean.Comment;
import com.cskaoyan.bean.CommentExample;
import com.cskaoyan.bean.User;
import com.cskaoyan.bean.comment.*;
import com.cskaoyan.mapper.CommentMapper;
import com.cskaoyan.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: heliming
 * @date: 2021/6/7
 * @description:
 */
@Service
public class WXCommentServiceImpl implements WXCommentService {

    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public WXCommentList selectList(Integer valueId, Byte type, Integer size, Integer page, Integer showType) {
        WXCommentList wxCommentList = new WXCommentList();
        CommentExample commentExample = new CommentExample();
        int count = 0;
        if (showType == 0) {//全部评论
            if (type == 0) {//商品评论
                commentExample.or().andValueIdEqualTo(valueId).andTypeEqualTo(type).andDeletedEqualTo(false);
                List<Comment> comments = commentMapper.selectByExample(commentExample);
                ArrayList<DataBean> dataBeans = new ArrayList<>();
                for (Comment comment : comments) {
                    DataBean dataBean = new DataBean();
                    UserInfoBean userInfoBean = new UserInfoBean();
                    dataBean.setAddTime(comment.getAddTime());
                    dataBean.setContent(comment.getContent());
                    dataBean.setPicList(comment.getPicUrls());


                    User user = userMapper.selectByPrimaryKey(comment.getUserId());
                    userInfoBean.setNickName(user.getNickname());
                    userInfoBean.setAvatarUrl(user.getAvatar());
                    dataBean.setUserInfo(userInfoBean);


                    dataBeans.add(dataBean);
                    count++;
                }
                wxCommentList.setData(dataBeans);

            } else if (type == 1) {//专题评论

                PageHelper.startPage(page,size);
                commentExample.setOrderByClause("add_time desc");
                commentExample.or().andValueIdEqualTo(valueId).andTypeEqualTo(type).andDeletedEqualTo(false);

                List<Comment> comments = commentMapper.selectByExample(commentExample);
                ArrayList<DataBean> dataBeans = new ArrayList<>();
                for (Comment comment : comments) {
                    DataBean dataBean = new DataBean();
                    UserInfoBean userInfoBean = new UserInfoBean();
                    dataBean.setAddTime(comment.getAddTime());
                    dataBean.setContent(comment.getContent());
                    dataBean.setPicList(comment.getPicUrls());

                    User user = userMapper.selectByPrimaryKey(comment.getUserId());
                    if (user == null) {
                        continue;
                    }
                    userInfoBean.setNickName(user.getNickname());
                    userInfoBean.setAvatarUrl(user.getAvatar());
                    dataBean.setUserInfo(userInfoBean);


                    dataBeans.add(dataBean);
//                    count++;

                }
                PageInfo<Comment> commentPageInfo = new PageInfo<>(comments);
                long total = commentPageInfo.getTotal();
                wxCommentList.setCount((int) total);
                wxCommentList.setData(dataBeans);
                return wxCommentList;

            }

        } else if (showType == 1) {//有图评论
            commentExample.or().andValueIdEqualTo(valueId).andTypeEqualTo(type).andHasPictureEqualTo(true).andDeletedEqualTo(false);
            List<Comment> comments = commentMapper.selectByExample(commentExample);
            ArrayList<DataBean> dataBeans = new ArrayList<>();
            for (Comment comment : comments) {
                DataBean dataBean = new DataBean();
                UserInfoBean userInfoBean = new UserInfoBean();
                dataBean.setAddTime(comment.getAddTime());
                dataBean.setContent(comment.getContent());
                dataBean.setPicList(comment.getPicUrls());


                User user = userMapper.selectByPrimaryKey(comment.getUserId());
                userInfoBean.setNickName(user.getNickname());
                userInfoBean.setAvatarUrl(user.getAvatar());
                dataBean.setUserInfo(userInfoBean);
                dataBeans.add(dataBean);
                count++;

            }
            wxCommentList.setData(dataBeans);
        }
        wxCommentList.setCount(count);
        wxCommentList.setCurrentPage(page);


        return wxCommentList;
    }

    @Override
    public WXCommentCount selectCount(Integer valueId, Integer type) {

        WXCommentCount wxCommentCount = new WXCommentCount();
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andValueIdEqualTo(valueId).andTypeEqualTo((byte)0);

        List<Comment> comments = commentMapper.selectByExample(commentExample);
        Integer hasPicCount = 0;
        Integer allCount = 0;
        for (Comment comment : comments) {
            if (comment.getHasPicture()) {
                hasPicCount++;
            }
            allCount++;

        }
        wxCommentCount.setAllCount(allCount);
        wxCommentCount.setHasPicCount(hasPicCount);

        return wxCommentCount;
    }

    @Override
    public WXCommentPostVO insertPost(WXCommentPostBO wxCommentPostBO, Integer userId) {
        WXCommentPostVO wxCommentPostVO = new WXCommentPostVO();
        Comment comment = new Comment();
        BeanUtils.copyProperties(wxCommentPostBO, comment);

        comment.setAddTime(new Date());
        comment.setUpdateTime(new Date());
        comment.setDeleted(false);
        comment.setUserId(userId);

        commentMapper.insertSelective(comment);
        BeanUtils.copyProperties(comment, wxCommentPostVO);
        wxCommentPostVO.setUserId(userId);

        return wxCommentPostVO;
    }
}
